# nginx日志生成脚本
# 127.0.0.1 - - [24/Mar/2024:21:57:01 +0800]
# "GET /api/sms/send HTTP/1.1" 200 80 "-"
# "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
# AppleWebKit/537.36 (KHTML, like Gecko)
# Chrome/122.0.0.0 Safari/537.36"
import random
import numpy
import fake_useragent
import os


def generate_normal_randoms(
        num: int,
        min_value: int,
        max_value: int,
        mean: int,
        std: int
):
    """
    生成正态分布随机数序列
    :param num: 生成数量
    :param min_value: 最小值
    :param max_value: 最大值
    :param mean: 均值
    :param std: 方差
    :return:
    """
    # 生成正态分布随机数
    random_numbers = numpy.random.normal(mean, std, num)
    # 转换成整数
    random_ints = numpy.round(random_numbers).astype(int)
    # 归一化
    randomints = numpy.clip(random_ints, min_value, max_value)

    return randomints


class generator:
    ip_pool = []
    url_pool = []

    @classmethod
    def generate_ip(cls, number: int = 1000):
        """
        生成IPv4地址
        例如： 10.0.11.2
        :return:
        """
        for i in range(number):
            n1 = random.choice(range(1, 254))
            n2 = random.choice(range(1, 254))
            n3 = random.choice(range(1, 254))
            n4 = random.choice(range(1, 254))
            cls.ip_pool.append("%d.%d.%d.%d" % (n1, n2, n3, n4))

        return cls.ip_pool

    @classmethod
    def generate_url(cls, moduels):
        """
        生成访问地址
        :return:
        """
        for v in moduels:
            cls.url_pool.append("/api/" + v + "/index")
            cls.url_pool.append("/api/" + v + "/add")
            cls.url_pool.append("/api/" + v + "/edit")
            cls.url_pool.append("/api/" + v + "/del")
            cls.url_pool.append("/api/" + v + "/import")
            cls.url_pool.append("/api/" + v + "/export")

        return cls.url_pool

    @classmethod
    def gen(cls, number: int = 100, y: int = 2024, m: str = 'May', d: int = 1, target=''):
        """
        生成日志
        :param number: 数量
        :param y: 年
        :param m: 月
        :param d: 日
        :param target: 日志文件路径
        :return:
        """
        ua = fake_useragent.UserAgent()

        normal_rand_numbers = generate_normal_randoms(number, 0, 23, 10, 5)

        logs = []
        for i in range(number):
            log = '%s -- [%d/%s/%d:%02d:%02d:%02d +0800] "%s %s HTTP/1.1" 200 %d "-" "%s"' % (
                random.choice(cls.ip_pool),
                d, m, y,
                normal_rand_numbers[i],
                random.randint(0, 59),
                random.randint(0, 59),
                random.choice(['GET', 'POST']),
                random.choice(cls.url_pool),
                random.randint(100, 1000),
                ua.random
            )
            logs.append(log)

        if target != '':
            with open(target, 'a', encoding='utf8') as f:
                for v in logs:
                    f.write(v + "\n")


months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

ROOT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(os.path.join(__file__, '.'))))

if __name__ == '__main__':
    #生成10000个IP
    generator.generate_ip(10000)
    #生成URL地址
    moduels = ['user', 'good', 'book', 'org', 'post', 'person', 'order', 'index', 'page', 'my']
    generator.generate_url(moduels)
    # 生成2024年1月访问日志, 每天日志数量 1000~5000
    y = 2024
    m = 1

    filePath = ROOT_PATH + '/test/nginx_logs/access_%04d%02d.log' % (y, m)
    for d in range(1, 31 + 1):

        generator.gen(random.randint(1000, 5000), 2024, months[m - 1], d, filePath)
        print(filePath)

    # 生成2024年2月访问日志, 每天日志数量 1000~5000
    m = 2
    filePath = ROOT_PATH + '/test/nginx_logs/access_%04d%02d.log' % (y, m)
    for d in range(1, 29 + 1):
        generator.gen(random.randint(1000, 5000), 2024, months[m - 1], d, filePath)
        print(filePath)
